#include "stdafx.h"
#include "NodeList.h"

DM_NodeList::~DM_NodeList()
{
	removeAll();
}

void DM_NodeList::removeAll()
{
	DM_NodeListDef *cur, *tmp;
	for( cur = m_beg; cur != NULL; )
	{
		tmp = cur;
		cur = cur->next;
		tmp->ptArray.RemoveAll();
		delete tmp;
	}
	m_beg = m_end = NULL;
}

DM_NodeListDef* DM_NodeList::find( dmDbEntity* pdmEntity, DM_NodeListDef *&prev, DM_NodeListDef *&next )
{
	DM_NodeListDef *cur;
	prev = next = NULL;
	for( cur = m_beg; cur != NULL; cur = cur->next )
	{
		if ( cur->pdbEntity==pdmEntity)
		{
			next = cur->next;
			break;
		}
		prev = cur;
	}
	if( !cur )
		prev = NULL;
	return cur;
}

DM_NodeListDef *m_cur=NULL;

void DM_NodeList::add( dmDbEntity* pdmEntity,  dmDPoint pt,int type)
{
	DM_NodeListDef *cur, *prev, *next;
	if( cur = find( pdmEntity, prev, next ) )
	{
		cur->ptArray.Add( Node(pt,type) );
		m_cur=cur;
	}
	else
	{
		cur = new DM_NodeListDef( pdmEntity );
		if( cur )
		{
			if( m_beg )
			{
				ASSERT( m_end );
				m_end->next = cur;
				m_end = cur;
			}
			else
			{
				ASSERT( !m_end );
				m_beg = m_end = cur;
			}
			cur->ptArray.Add( Node(pt,type) );
		}
		m_cur=cur;
	}
}

void DM_NodeList:: add(dmDbEntity* pdmEntity, CEntNodesArray& entArray)
{
	DM_NodeListDef *cur, *prev, *next;
	if( cur = find( pdmEntity, prev, next ) )
	{		
		m_cur=cur;
	}
	else
	{
		cur = new DM_NodeListDef( pdmEntity );
		if( cur )
		{
			if( m_beg )
			{
				ASSERT( m_end );
				m_end->next = cur;
				m_end = cur;
			}
			else
			{
				ASSERT( !m_end );
				m_beg = m_end = cur;
			}
		//	cur->ptArray.Add( pt[] );
		}
		m_cur=cur;
	}

	if(cur!=NULL)
	{
		for(int i=0;i<entArray.GetCount();i++)
		{
			cur->ptArray.Append(entArray );
		}
	}


}

void DM_NodeList::add(dmDbEntity* pdmEntity, dmDPoint* pt,int count,int type)
{

	DM_NodeListDef *cur, *prev, *next;
	if( cur = find( pdmEntity, prev, next ) )
	{		
		m_cur=cur;
	}
	else
	{
		cur = new DM_NodeListDef( pdmEntity );
		if( cur )
		{
			if( m_beg )
			{
				ASSERT( m_end );
				m_end->next = cur;
				m_end = cur;
			}
			else
			{
				ASSERT( !m_end );
				m_beg = m_end = cur;
			}
		//	cur->ptArray.Add( pt[] );
		}
		m_cur=cur;
	}

	if(cur!=NULL)
	{
		for(int i=0;i<count;i++)
		{
			cur->ptArray.Add( Node(pt[i],type) );
		}
	}

}

///Fix by Qin
void DM_NodeList::addBy(dmDbEntity* pdmEntity,  dmDPoint pt,int type)
{
	if(!m_cur) return;
	m_cur->ptArray.Add( Node(pt,type) );
}

void DM_NodeList::remove( dmDbEntity* pdmEntity )
{
	DM_NodeListDef *cur, *prev, *next;

	if( cur = find( pdmEntity, prev, next ) )
	{
		if( prev && next )
			prev->next = next;
		else if( !prev && !next )
		{
			m_beg = m_end = NULL;
		}
		else if( !prev )
		{
			m_beg = next;
		}
		else if( !next )
		{
			prev->next = NULL;
			m_end = prev;
		}

		// delete found element with its array of points
		cur->ptArray.RemoveAll();
		delete cur;
	}
}

void DM_NodeList::begin()
{
	m_cur = m_beg;
	m_ptIdx = 0;
}

BOOL DM_NodeList::getPair( dmDbEntity*& pdmEntity, dmDPoint& pt ,int &index)
{
	for( ; m_cur != NULL; m_cur = m_cur->next )
	{
		int numOfPoints = m_cur->ptArray.GetSize();
		while( m_ptIdx < numOfPoints )
		{			
			pdmEntity=m_cur->pdbEntity;
			Node node= m_cur->ptArray.GetAt(m_ptIdx);

			pt=node.m_pt;
			
			index=m_ptIdx++;
			
			return TRUE;
		}
		m_ptIdx = 0;
	}
	return FALSE;
}


